home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / advanced97 / TEXMOVIE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  4.0 KB  |  171 lines

  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <GL/glut.h>
  5. #include "texture.h"
  6.  
  7. #if !defined(GL_VERSION_1_1) && !defined(GL_VERSION_1_2)
  8. #define glBindTexture    glBindTextureEXT
  9. #endif
  10.  
  11. static int the_texture;
  12. static int texture_count;
  13. static int shrink = 1;
  14.  
  15. void afunc(void) {
  16.     static int state;
  17.     if (state ^= 1)
  18.     glEnable(GL_ALPHA_TEST);
  19.     else
  20.     glDisable(GL_ALPHA_TEST);
  21. }
  22.  
  23. void bfunc(void) {
  24.     static int state;
  25.     if (state ^= 1)
  26.     glEnable(GL_BLEND);
  27.     else
  28.     glDisable(GL_BLEND);
  29. }
  30.  
  31. void sfunc(void) {
  32.     shrink ^= 1;
  33. }
  34.  
  35. void tfunc(void) {
  36.     static int state;
  37.     if (state ^= 1)
  38.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  39.     else
  40.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  41. }
  42.  
  43. void fourfunc(void) {
  44.     static int state;
  45.     GLenum wrap;
  46.     int i;
  47.  
  48.     glMatrixMode(GL_TEXTURE);
  49.     if (state ^= 1) {
  50.     wrap = GL_REPEAT;
  51.     glScalef(4.f, 4.f, 1.f);
  52.     } else {
  53.     wrap = GL_CLAMP;
  54.     glLoadIdentity();
  55.     }
  56.     glMatrixMode(GL_MODELVIEW);
  57.  
  58.     for(i = 0; i < texture_count; i++) {
  59.     glBindTexture(GL_TEXTURE_2D, i+1);
  60.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap);
  61.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);
  62.     }
  63. }
  64.  
  65. void help(void) {
  66.     printf("Usage: texmovie image0 ... imagen\n");
  67.     printf("'h'            - help\n");
  68.     printf("'a'            - toggle alpha test\n");
  69.     printf("'b'            - toggle blend\n");
  70.     printf("'s'            - toggle shrink\n");
  71.     printf("'t'            - toggle MODULATE or REPLACE\n");
  72.     printf("'4'            - toggle repeat by 4\n");
  73. }
  74.  
  75. void init(int argc, char *argv[]) {
  76.     unsigned *image;
  77.     int i, width, height, components;
  78.  
  79.     glEnable(GL_TEXTURE_2D);
  80.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  81.  
  82.     for(i = 0; i < argc; i++) {
  83.     image = read_texture(argv[i], &width, &height, &components);
  84.     if (image == NULL) {
  85.         fprintf(stderr, "Error: Can't load image file \"%s\".\n",
  86.             argv[i]);
  87.         exit(EXIT_FAILURE);
  88.     } else {
  89.         printf("%d x %d image loaded\n", width, height);
  90.     }
  91.     glBindTexture(GL_TEXTURE_2D, i+1);
  92.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  93.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  94.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  95.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  96.     glTexImage2D(GL_TEXTURE_2D, 0, components, width,
  97.                  height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
  98.     texture_count++;
  99.     }
  100.  
  101.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  102.     glEnable(GL_TEXTURE_2D);
  103.     glClearColor(.25f, .25f, .25f, .25f);
  104.  
  105.     glAlphaFunc(GL_GREATER, 0.f);
  106.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  107. }
  108.  
  109. void
  110. animate(void) {
  111.  
  112.     the_texture++;
  113.     if (the_texture >= texture_count) the_texture = 0;
  114.  
  115.     glutPostRedisplay();
  116. }
  117.  
  118. void display(void) {
  119.     glClear(GL_COLOR_BUFFER_BIT);
  120.  
  121.     glBindTexture(GL_TEXTURE_2D, the_texture+1);
  122.     glPushMatrix();
  123.     if (shrink) glScalef(.5f, .5f, 1.f);
  124.     glBegin(GL_POLYGON);
  125.     glTexCoord2f(0.0, 0.0);
  126.     glVertex2f(-1.0, -1.0);
  127.     glTexCoord2f(1.0, 0.0);
  128.     glVertex2f(1.0, -1.0);
  129.     glTexCoord2f(1.0, 1.0);
  130.     glVertex2f(1.0, 1.0);
  131.     glTexCoord2f(0.0, 1.0);
  132.     glVertex2f(-1.0, 1.0);
  133.     glEnd();
  134.     glPopMatrix();
  135.     glutSwapBuffers();
  136. }
  137.  
  138. void reshape(int w, int h) {
  139.     glViewport(0, 0, w, h);
  140. }
  141.  
  142. /* ARGSUSED1 */
  143. void
  144. key(unsigned char key, int x, int y) {
  145.     switch(key) {
  146.     case 'a': afunc(); break;
  147.     case 'b': bfunc(); break;
  148.     case 'h': help(); break;
  149.     case 's': sfunc(); break;
  150.     case 't': tfunc(); break;
  151.     case '4': fourfunc(); break;
  152.     case '\033': exit(EXIT_SUCCESS); break;
  153.     default: break;
  154.     }
  155.     glutPostRedisplay();
  156. }
  157.  
  158. int main(int argc, char** argv) {
  159.     glutInitWindowSize(256, 256);
  160.     glutInit(&argc, argv);
  161.     glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
  162.     (void)glutCreateWindow(argv[0]);
  163.     init(argc-1, argv+1);
  164.     glutDisplayFunc(display);
  165.     glutKeyboardFunc(key);
  166.     glutReshapeFunc(reshape);
  167.     glutIdleFunc(animate);
  168.     glutMainLoop();
  169.     return 0;
  170. }
  171.